function core(){ 
    $(window).scroll(function () { 
        $("div.minimenuCatPosition").stop(true,true).hide();    
    });
    
    $('a[name=modal]').live("click",function(e) {                    
        e.preventDefault();
                    
        var id = "#"+$(this).attr('href');
        
        upadteMask();       
        
                    
        $('#mask').fadeTo("fast",0.85); 
                    
        var winH = $(window).height();
        var winW = $(window).width();
               
     
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);
                    
        $(id).fadeIn(500);
                    
        var type = $(id).attr("type");
        if (type == "velikosti"){
            $("div."+$(this).attr('href')).html('<img src="templates/eshop/imgs/LoadingIcon-White.gif" />').load("eshopScript/AdminAjax.php?velikosti=&idmap="+$(this).attr('href'));
        }
        if (type == "barvy"){
            $("div."+$(this).attr('href')).html('<img src="templates/eshop/imgs/LoadingIcon-White.gif" />').load("eshopScript/AdminAjax.php?barvy=&idmap="+$(this).attr('href'));
        }
        if (type == "image"){
            var uploader = new qq.FileUploader({
                element: document.getElementById('file-uploader_'+$(this).attr('href')),                
                action: 'eshopScript/FileUpload.php',
                params: {
                    id: $(this).attr('href')                                
                }                            
            });                        
        }
                    
        if (type == "section"){
            $("#listOfSections").html('<img src="templates/eshop/imgs/LoadingIcon-White.gif" />').load("eshopScript/Ajax.php?sectionList=");
        }                    
        if (type == "editSection"){
            $("select."+$(this).attr('href')).html('<img src="templates/eshop/imgs/LoadingIcon-White.gif" />').load("eshopScript/Ajax.php?sectionListFull="+$(this).attr("data-parent"));
        }
    });
     
    $('.window .close').live("click",function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });    
     
    $('#mask').live("click",function () {
        $(this).hide();
        $('.window').hide();
    }); 
    
    $("span.editSave").live("click",function (e){        
        id = $(this).attr("data-name");
        form = $("#"+id+"_form");
        form.find(".textAreaCkEditor").each(function(){    
            $("#"+$(this).attr("id")+"_hiddenInput").remove();
            var textName = $(this).attr("id").split("_ckeditor");
            textName=textName[0];                       
            contentNotEscaped = $(this).val();            
            //parsovani nahledu z textu a ulozeni do skryte pripravenoho inputu
            nahledHidden = form.find(".nahledHidden");
            nahledHiddenValue = "";
            nahledStart = contentNotEscaped.indexOf("|");
            if (nahledStart != -1){
                nahledEnd = contentNotEscaped.indexOf("|",nahledStart+1);
                if (nahledEnd != -1){
                    nahledHiddenValue = escape(contentNotEscaped.substring(nahledStart+1, nahledEnd));
                    contentNotEscaped = contentNotEscaped.replace("|","").replace("|","");    
                }
            }     
            if (nahledHidden != null){
                nahledHidden.attr("value",nahledHiddenValue);
            }  
            //escapnuti
            content = escape(contentNotEscaped);
            form.append('<input id="'+$(this).attr("id")+'_hiddenInput" type="hidden" name="'+textName+'" value="'+content+'"/>') ;            
            
        });
              
        text = form.serialize();
        $.post("index.php", text, function(data) {
            location.reload(true);
        });   
        $('#mask').hide();
        $('.window').hide();
    }); 
    
    //msg
    if ($("#msg").html() != ""){
        alert($("#msg").html());
    }
    //msg   
    var initHash = true;
    $(window).hashchange( function(){        
        if (initHash) initClanky();
        initHash = false;
        var hash = location.hash.replace("#","");  
        while (true){
            if (hash.indexOf("s") > -1){
                hash = hash.replace("s","");
                parentDiv = $("div[data-clanekName='"+hash+"']");
                vysouvac = parentDiv.find(".clanekRozbalVysouvac");
                nahled = vysouvac.prev();
                minusPlus = parentDiv.find(".minusPlus");
                vysouvac.stop(true,true);                
                minusPlus.html("-");
                vysouvac.show("fast");                 
                nahled.hide();
                goToByScrollAttr(parentDiv.attr("data-clanekName"));
                break;
            }
            if (hash.indexOf("h") > -1){
                hash = hash.replace("h","");
                parentDiv = $("div[data-clanekName='"+hash+"']");
                vysouvac = parentDiv.find(".clanekRozbalVysouvac");
                nahled = vysouvac.prev();
                minusPlus = parentDiv.find(".minusPlus");
                vysouvac.stop(true,true);                                
                minusPlus.html("+");
                vysouvac.hide("fast");                
                nahled.show();
                goToByScrollAttr(parentDiv.attr("data-clanekName"));
                break;
            }
            break;
        }        
    })
    
    $(window).hashchange();
    createMiniMenu();
    initButtons();
    initImageUpload();
    initDatePicker();
    initAddLink();
    initVisibility();
}

function goToByScroll(classID){
    $('html,body').animate({
        scrollTop: $("."+classID).offset().top
    },'slow');
}
function goToByScrollAttr(tagDataClanekName){
    $('html,body').animate({
        scrollTop: $("div[data-clanekName="+tagDataClanekName+"]").offset().top
    },'slow');
}
function adjustClanekVisibility(tag){
    visib = tag.attr("data-visibility");
    if (visib == "0"){
        tag.find(".clanekRozbalNadpis").fadeTo("slow",0.33);
    }
    if (visib == "1"){
        tag.find(".clanekRozbalNadpis").fadeTo("slow",1);
    }
}
function initVisibility(){
    $(".clanekRozbal").each(function(){        
        adjustClanekVisibility($(this));
    });
    $(".clanekRozbalPorad").each(function(){        
        adjustClanekVisibility($(this));
    });
    $(".visibilityLink").live("click",function(e){
        e.preventDefault();
        $("#blind").load("index.php?ajax=visibilityCat&numberName="+$(this).attr("href")+"&visiblityData="+$(this).attr("data-visibility"),function(){
            refresh();
        });    
    });
}

function refresh(){
    location.reload(true);
}

function initAddLink(){
    $(".addLink").live("click",function(e){
        e.preventDefault();
        $("#blind").load("index.php?ajax=addCat&catName="+$(this).attr("href"),function(){
            refresh();
        });
    });
}

function initDatePicker(){
    $(".datepicked").each(function(){
        $(this).glDatePicker();
    });    
}

function initImageUpload(){
    $(".file-uploader").each(function(){
        maxNotReached = $(this).find(".maxNotReached").html();
        maxReached = $(this).find(".maxReached").html();
        imageName = $(this).attr("data-imageId");
        id = $(this).attr("id");
        newFileUploader = new qq.FileUploader({
            element: document.getElementById(id),                
            action: 'index.php?ajax=uploadCismap',
            params: {
                id: imageName
            },         
            onSubmit: function(id, fileName){
                $(".loadingGif").show();
            },
            onComplete: function(id, fileName, responseJSON){ 
                loadAttach(id,fileName,responseJSON);
                $(".loadingGif").hide();
            }
        });  
        $(this).find(".qq-upload-button").append(maxNotReached);
        $(this).after('<div class="maxReached hidden">'+maxReached+'</div>');
    });
}



function initButtons(){        
    $("div.button").each(function(){                        
        $(this).live("click",function(e){
            e.preventDefault();
            $(window.location).attr('href', $(this).find("a").attr("href"));
        });
    });
}
var parentCounter = 0;
//editLinkVisibility
function createMiniMenu(){    
    $("div.minimenu").each(function(){    
        var a = $(this);             
        var type = a.attr("data-type");
        var specialClass = a.attr("data-specialClass");
        var parentElement = a.parent();
        var parent = parentElement.attr("id");
        if (parent == undefined) {
            parent = "parentCounter"+parentCounter;
            parentElement.attr("id", parent);
            parentCounter++;
        }
        var divCat;
        if (type == "always-visible"){
            if ($("#minimenuDiv_"+parent).html() == null){
                parentElement = $("#"+parent);
                parentElement.addClass("inline-table");
                parentElement.append('<div id="minimenuDiv_'+parent+'" class="'+specialClass+' minimenuCatPositionAlwaysVisible floatRight "></div>');            
                divCat = $("#minimenuDiv_"+parent);                                       
            }else{
                divCat = $("#minimenuDiv_"+parent);
            }                    
            var ahtml = a.html();
            a.remove();
            divCat.append(ahtml); 
        }else{
            if ($("#minimenuDiv_"+parent).html() == null){
                parentElement = $("#"+parent);
                if (parentElement.attr("data-type") == "noinline"){
                        
                }else{
                    parentElement.addClass("inline-table");
                }
                parentElement.append('<div id="minimenuDiv_'+parent+'" class="'+specialClass+' hidden minimenuCatPosition floatRight fixedPos "></div>');            
                divCat = $("#minimenuDiv_"+parent);                    
                parentElement.die("mouseenter",miniMenuMouseEnter);
                parentElement.live("mouseenter",{
                    divCat : divCat , 
                    parent : parentElement
                },miniMenuMouseEnter);
                divCat.die("hover",miniMenuHover);
                divCat.live("hover",{
                    divCat : divCat
                },miniMenuHover);
            }else{
                divCat = $("#minimenuDiv_"+parent);
            }                    
            var ahtml = a.html();
            a.remove();
            divCat.append(ahtml);                                    
        }
    });
}
function miniMenuHover(e){
    var divCat = e.data.divCat;          
    divCat.stop(true,true).show();  
}
var visibleEditElement = null;   
function miniMenuMouseEnter(e){
    var divCat = e.data.divCat;                
    var parent = e.data.parent;                
    var x = e.clientX;
    var y = e.clientY;
    if (divCat.is(":hidden") == true ){  
        if (visibleEditElement != null){
            visibleEditElement.attr("style","display:block;");
        }
        $("div.minimenuCatPosition").stop(true,true).hide();
        divCat.stop(true,true).show();            
        visibleEditElement=divCat;
        correctMinimenuPosition();
    }
        
}


    
function correctMinimenuPosition(){        
    newY = visibleEditElement.parent().offset().top-$(window).scrollTop();
    newX = visibleEditElement.parent().offset().left-$(window).scrollLeft()+visibleEditElement.parent().width();
    visibleEditElement.css({
        left: newX,
        top: newY
    })
}
//editLinkVisibility

//numberValidation
function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}
//numberValidation

function noEdit(evt)
{
    return false;
}

function upadteMask(){
    maskHeight = $(document).height();
    maskWidth = $(window).width();
                    
    $('#mask').css({
        'width':maskWidth,
        'height':maskHeight
    });
}



